package adminService

import (
	"fmt"
	"gin-gorm-oj/define"
	"gin-gorm-oj/helper"
	"gin-gorm-oj/models"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"net/http"
	"strconv"
)

type LoginRequest struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
}

type EmployeeDTO struct {
	Id       int    `json:"id" binding:"required" structs:"id"`
	Username string `json:"username" binding:"required" structs:"username"`
	Name     string `json:"name" binding:"required" structs:"name"`
	Phone    string `json:"phone" binding:"required" structs:"phone"`
	Sex      string `json:"sex" binding:"required" structs:"sex"`
	IdNumber string `json:"id_number" binding:"required" structs:"id_number"`
}
type EmployeeLoginVO struct {
	Id       int    `json:"id"`
	Username string `json:"username"`
	Name     string `json:"name"`
	Token    string `json:"token"`
}

func Register(c *gin.Context) {
	fmt.Println("调用Register")
	var employeeDTO EmployeeDTO
	err := c.ShouldBindBodyWithJSON(&employeeDTO)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	employee := &models.Employee{
		IdNumber: employeeDTO.IdNumber,
		Username: employeeDTO.Username,
		Name:     employeeDTO.Name,
		Phone:    employeeDTO.Phone,
		Sex:      employeeDTO.Sex,
		Status:   define.DEFAULT_STATUS_ENABLE,
		Password: helper.GetMd5(define.DEFAULT_PASSWORD),
	}
	err = models.Db.Create(employee).Error
	if err != nil {
		fmt.Println("插入数据失败", err)
	}

	fmt.Println(employeeDTO)

}

func Login(c *gin.Context) {
	fmt.Println("请求Login")
	var loginRequest LoginRequest
	err := c.ShouldBindJSON(&loginRequest)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 600,
			"msg":  "参数错误",
		})
		return
	}
	employee := new(models.Employee)
	err = models.Db.Where("username = ?", loginRequest.Username).First(&employee).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "用户名或密码错误",
		})
		return
	}
	if helper.GetMd5(loginRequest.Password) != employee.Password {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "用户名或密码错误",
		})
		return
	}
	if employee.Status == define.DEFAULT_STATUS_DISABLE {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "用户被禁用",
		})
		return
	}
	token, err := helper.GenerateToken(int(employee.ID))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "token生成失败",
		})
		return
	}
	employeeLoginVO := EmployeeLoginVO{
		Id:       int(employee.ID),
		Username: employee.Username,
		Name:     employee.Name,
		Token:    token,
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": employeeLoginVO,
	})
}

func UpdateEmployeeStatus(c *gin.Context) {
	status, err := strconv.Atoi(c.Param("status"))

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	dbEmployee := new(models.Employee)
	err = models.Db.Model(new(models.Employee)).Where("id = ?", id).First(&dbEmployee).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	employee := &models.Employee{
		Status: status,
	}

	err = models.Db.Model(new(models.Employee)).Where("id = ?", id).Updates(&employee).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "更新失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "Success",
	})
}

func GetEmployeeById(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	employee := new(models.Employee)
	err = models.Db.Model(new(models.Employee)).Where("id = ?", id).First(&employee).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": employee,
	})
}

func UpdateEmployee(c *gin.Context) {
	var employeeDTO EmployeeDTO
	err := c.ShouldBindBodyWithJSON(&employeeDTO)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	dbEmployee := new(models.Employee)
	err = models.Db.Model(new(models.Employee)).Where("id = ?", employeeDTO.Id).First(&dbEmployee).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}

	//employee := &models.Employee{
	//	IdNumber: employeeDTO.IdNumber,
	//	Username: employeeDTO.Username,
	//	Name:     employeeDTO.Name,
	//	Phone:    employeeDTO.Phone,
	//	Sex:      employeeDTO.Sex,
	//}
	employee := new(models.Employee)
	copier.Copy(&employee, &employeeDTO)
	err = models.Db.Model(new(models.Employee)).Where("id = ?", employeeDTO.Id).Updates(&employee).Error
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "更新失败",
		})
		return
	}
	employeeLatest := new(models.Employee)
	models.Db.Model(new(models.Employee)).Where("id = ?", employeeDTO.Id).First(&employeeLatest)
	c.JSON(http.StatusBadRequest, gin.H{
		"code": 200,
		"msg":  "Update Success",
		"data": employeeLatest,
	})

}

func EmployeePageList(c *gin.Context) {
	name := c.Query("name")
	page, err := strconv.Atoi(c.DefaultQuery("page", define.DefaultPage))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", define.DefaultSize))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	page = (page - 1) * pageSize

	var total int64
	db := models.Db.Model(new(models.Employee))
	if name != "" {
		db.Where("name like ?", "%"+name+"%")
	}
	var employees []models.Employee
	err = db.Count(&total).Offset(page).Limit(pageSize).Find(&employees).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": gin.H{
			"total":   total,
			"records": employees,
		},
	})
	return
}
